package sg.edu.nus.iss.ooad.jass.dao.pattern;

import java.io.IOException;
import java.util.Enumeration;
import java.util.Properties;
import java.util.ResourceBundle;
import sg.edu.nus.iss.ooad.jass.dao.IAdvertiserDAO;
import sg.edu.nus.iss.ooad.jass.dao.ICompanyDAO;
import sg.edu.nus.iss.ooad.jass.dao.IJobAdvertisementDAO;
import sg.edu.nus.iss.ooad.jass.dao.IJobseekerDAO;
import sg.edu.nus.iss.ooad.jass.dao.IResponseDAO;

/**
 *
 * This factory class supplies the underlying DAO implementation class based on
 * the DAO class specified in the system.properties
 *
 * @author Isak Rabin (HT082193M)
 * @version 1.0
 * @since 1.0
 *
 * Development Environment : Netbeans IDE v6.7
 * Name of the File        : AbstractJASSDAOFactory.java
 * Creation / Modification History
 *         Isak Rabin        20-Sep-2009        Created
 *
 */
public abstract class AbstractJASSDAOFactory {

        //List of all supported DAO Type
        public static final int FILE_DATABASE = 0;
        public static final int MYSQL_DATABASE = 1;
        public static final int SQL_DATABASE = 2;
        public static final int ORACLE_DATABASE = 3;

        //AdvertiserDAO interface
        public abstract IAdvertiserDAO getAdvertiserDAO();

        // CompanyDAO interface
        public abstract ICompanyDAO getCompanyDAO();

        // JobseekerDAO interface
        public abstract IJobseekerDAO getJobseekerDAO();

        // JobAdvertisementDAO interface
        public abstract IJobAdvertisementDAO getJobAdvertisementDAO();

        // ResponseDAO interface
        public abstract IResponseDAO getResponseDAO();
        // Member variable to hold the JASSDAO implementation class name
        public static String jassDAOClassName = null;
        // Interface to hold JASS DAO implementation
        private static AbstractJASSDAOFactory dao = null;

        static {
                Properties prop = null;
                try {
                        // Load the jass.properties file
                        prop = loadParams("jass");
                        // Get the value set for "DAOImplementationClass" key in the DAOImplementation.properties file
                        jassDAOClassName = (String) prop.get("DAOImplementationClass");
                } catch (Exception ex) {
                        System.out.println("Fatal Error : Couldn't Read Properties file : " + ex.toString());
                        ex.printStackTrace();
                        // In case of exception set the default DAO Implementation class name
                        jassDAOClassName = "sg.edu.nus.iss.ooad.jass.JASSDAOFactoryPropertiesImpl";
                } finally {
                        prop = null;
                }
        }

        /**
         * This method will return the Type of DAO implementation
         *
         * @param whichFactory The type of implementation of persistent.
         * @return Implemented Persistent DAOFactory.
         */
        public static AbstractJASSDAOFactory getJASSDAO() {

                if (dao == null) {
                        int whichFactory = Integer.parseInt(jassDAOClassName);
                        switch (whichFactory) {
                                case FILE_DATABASE:
                                        dao = new JASSDAOFactoryPropertiesImpl();
                                case MYSQL_DATABASE:
                                        dao = new JASSDAOFactoryOracleImpl();
                                case SQL_DATABASE:
                                        dao = new JASSDAOFactorySQLImpl();
                                case ORACLE_DATABASE:
                                        dao = new JASSDAOFactoryMySQLImpl();
                                default:
                                        dao = new JASSDAOFactoryPropertiesImpl();
                        }
                }

                return dao;

        }

        /**
         * This method Loads a ResourceBundle and creates Properties from it
         *
         * @param file The filename
         * @return Properties object
         * @throws IOException
         */
        public static Properties loadParams(final String filename) throws IOException {

                // Loads a ResourceBundle and creates Properties from it
                Properties prop = new Properties();
                ResourceBundle bundle = ResourceBundle.getBundle(filename);
                Enumeration keys = bundle.getKeys();
                String key = null;
                while (keys.hasMoreElements()) {
                        key = (String) keys.nextElement();
                        prop.put(key, bundle.getObject(key));
                }

                return prop;
        }
}
